package easycloud.ecs.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;

import javax.swing.table.AbstractTableModel;

import com.google.common.collect.Multimap;

import easycloud.ecs.api.IProvider;
import easycloud.ecs.service.MonitorService;
import easycloud.ecs.service.ServiceException;

public class SyncTableModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4028779708945871789L;

	@SuppressWarnings("rawtypes")
	private List<List> data;

	private String[] columnNames;

	public SyncTableModel() {
		columnNames = new String[2];
		columnNames[0] = "Folder";
		columnNames[1] = "Providers";
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void addFolder(File file) {
		List rowData = new ArrayList();
		rowData.add(file.getAbsolutePath());
		for (int i = 1; i < columnNames.length; i++) {
			rowData.add(false);
		}
		if (data == null) {
			data = new ArrayList<List>();
		}
		data.add(rowData);
		fireTableDataChanged();
	}

	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Class getColumnClass(int column) {
		return (getValueAt(0, column).getClass());
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public String getColumnName(int col) {
		return columnNames[col];
	}

	@Override
	public int getRowCount() {
		if (data != null) {
			return data.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (data != null) {
			return data.get(rowIndex).get(columnIndex);
		} else {
			return null;
		}
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		return (column != 0);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setTableContent(Multimap<File, IProvider> syncDirs) {
		Collection<IProvider> providers = syncDirs.values();
		if (columnNames.length == 2) {
			setTableHeader(providers);
		}

		data = new ArrayList<List>(syncDirs.size());
		for (Entry<File, Collection<IProvider>> entry : syncDirs.asMap()
				.entrySet()) {
			List rowData = new ArrayList();
			rowData.add(entry.getKey().getAbsolutePath());
			StringBuffer sb = new StringBuffer();
			for (IProvider provider : entry.getValue()) {
				sb.append(provider.getName());
				sb.append(",");
			}
			for (String providerName : columnNames) {
				if ("Folder".equals(providerName)) {
					continue;
				}
				if (sb.indexOf(providerName) != -1) {
					rowData.add(true);
				} else {
					rowData.add(false);
				}
			}
			data.add(rowData);
		}
		fireTableDataChanged();
	}

	public void setTableHeader(Collection<IProvider> providers) {
		columnNames = new String[providers.size() + 1];
		columnNames[0] = "Folder";
		int columnIndex = 1;
		for (IProvider provider : providers) {
			columnNames[columnIndex++] = provider.getName();
		}
		fireTableStructureChanged();
	}

	@Override
	@SuppressWarnings("unchecked")
	public void setValueAt(Object value, int row, int column) {
		data.get(row).set(column, value);
		String providerName = columnNames[column];
		String filePath = data.get(row).get(0).toString();

		try {
			if (Boolean.TRUE.equals(value)) {
				MonitorService.getInstance().addSyncFolder(providerName,
						filePath);
			} else {
				MonitorService.getInstance().removeSyncFolder(providerName,
						filePath);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
}
